package com.aidong.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.Random;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder;

public class FuncUtil {
	private static Logger log = Logger.getLogger(FuncUtil.class);

	public FuncUtil() {
	}

	public static String Md5(String str) {
		try {
			return Md5(str.getBytes("utf-8"));
		} catch (UnsupportedEncodingException var2) {
			log.error(var2);
			return null;
		}
	}

	public static String Md5(byte[] b) {
		char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

		try {
			MessageDigest e = MessageDigest.getInstance("MD5");
			e.update(b);
			byte[] buf = e.digest();
			char[] str = new char[buf.length * 2];

			for(int i = 0; i < buf.length; ++i) {
				str[i * 2] = hexDigits[buf[i] >>> 4 & 15];
				str[i * 2 + 1] = hexDigits[buf[i] & 15];
			}

			return new String(str);
		} catch (Exception var6) {
			log.error(var6);
			return null;
		}
	}

	public static String HmacSha1(String data, String key) {
		byte[] buf = null;

		try {
			SecretKeySpec e = new SecretKeySpec(key.getBytes(), "HmacSHA1");
			Mac mac = Mac.getInstance("HmacSHA1");
			mac.init(e);
			buf = mac.doFinal(data.getBytes());
		} catch (Exception var5) {
			;
		}

		return (new BASE64Encoder()).encode(buf);
	}

	public static byte[] getFile(String path) {
		try {
			Object e = null;
			if(path.startsWith("http")) {
				URL os = new URL(path);
				HttpURLConnection buf = (HttpURLConnection)os.openConnection();
				buf.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)");
				buf.setRequestMethod("GET");
				buf.setConnectTimeout(15000);
				buf.setReadTimeout(15000);
				if(buf.getResponseCode() == 200) {
					e = buf.getInputStream();
				}
			} else {
				File os1 = new File(path);
				if(os1.exists()) {
					e = new FileInputStream(os1);
				}
			}

			if(e != null) {
				ByteArrayOutputStream os2 = new ByteArrayOutputStream();
				byte[] buf1 = new byte[1024];

				int n;
				while((n = ((InputStream)e).read(buf1)) != -1) {
					os2.write(buf1, 0, n);
				}

				((InputStream)e).close();
				return os2.toByteArray();
			}
		} catch (Exception var5) {
			log.warn("get file error: " + path);
		}

		return null;
	}

	public static String getLeft(String str, int len) {
		return getLeft(str, len, false);
	}

	public static String getLeft(String str, int len, boolean ad) {
		int n = 0;
		if(str != null && len > 0 && len < str.length() * 2) {
			if(ad) {
				len -= 3;
			}

			int i;
			for(i = 0; i < str.length(); ++i) {
				++n;
				if(str.charAt(i) > 127) {
					++n;
				}

				if(n >= len) {
					break;
				}
			}

			return i < str.length()?str.substring(0, i) + (ad?"...":""):str;
		} else {
			return str;
		}
	}

	public static String getRandNumStr(int len) {
		String ss = "0123456789";
		StringBuffer sb = new StringBuffer();
		Random rand = new Random(System.currentTimeMillis());

		for(int i = 0; i < len; ++i) {
			int n = rand.nextInt(10);
			sb.append(ss.substring(n, n + 1));
		}

		return sb.toString();
	}

	public static boolean strInArray(String str, String... values) {
		if(str != null && values.length != 0) {
			String[] arr$ = values;
			int len$ = values.length;

			for(int i$ = 0; i$ < len$; ++i$) {
				String v = arr$[i$];
				if(str.equals(v)) {
					return true;
				}
			}

			return false;
		} else {
			return false;
		}
	}
}
